package com.hqyj.fj.javaSpringBoot.modules.test.service.impl;

import com.hqyj.fj.javaSpringBoot.modules.account.entity.User;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.fj.javaSpringBoot.modules.test.dao.UserRepository;
import com.hqyj.fj.javaSpringBoot.modules.test.entity.Student;
import com.hqyj.fj.javaSpringBoot.modules.test.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional
    public Result<User> editUser(User user) {
        System.out.println(user.getUserName());
        userRepository.saveAndFlush(user);
        return new Result<User>(Result.ResultCode.SUCCESS.code,
                "Insert success.", user);

    }

    @Override
    @Transactional
    public Result<Object> insertUsers(List<User> userList) {
       userList.stream().forEach(item ->{
            entityManager.persist(item);
        });
        entityManager.flush();
        entityManager.clear();
        return  new Result<Object>(Result.ResultCode.SUCCESS.code,"insert success");
    }

    @Override
    public Result<Object> deleteStudentById(int id) {
        userRepository.deleteById(id);
        return new Result<Object>(Result.ResultCode.SUCCESS.code,
                "Delete success.");
    }

    @Override
    public User getUserById(int id) {
        return userRepository.getUserById(id);
}

    @Override
    public User getByUserNameAndEmail(String userName, String email) {
        return userRepository.findFirstByUserNameAndEmail(userName,email);
    }

    @Override
    public User getByUserNameAndPassword(String userName, String password) {
        return userRepository.findFirstByUserNameAndPassword(userName,password);
    }

    @Override
    public Page<User> getUserBySearchBean(SearchBean searchBean) {
        Sort.Direction direction = StringUtils.isBlank(searchBean.getDirection()) ||
                searchBean.getDirection().toLowerCase().equals("asc") ?
                Sort.Direction.ASC : Sort.Direction.DESC;
        Sort sort = Sort.by(direction, searchBean.getOrderBy());
        Pageable Pageable = PageRequest.of(
                searchBean.getCurrentPage() - 1,
                searchBean.getPageSize(),
                sort);

       User user = new User();
       user.setEmail(searchBean.getKeyword());
        ExampleMatcher matcher = ExampleMatcher
                .matchingAny()
                .withMatcher("userName", match -> match.contains())
                .withMatcher("email", match -> match.contains());
        Example example = Example.of(user, matcher);

        return userRepository.findAll(example, Pageable);
    }
}
